package guda.house.web.action.sys;

import guda.grape.autogen.common.page.BaseQuery;
import guda.grape.autogen.common.page.BizResult;
import guda.grape.util.RequestUtil;
import guda.house.biz.beans.vo.JsonResult;
import guda.house.biz.query.BuildingQuery;
import guda.house.biz.query.SuiteQuery;
import guda.house.biz.query.UnitQuery;
import guda.house.biz.query.VillageQuery;
import guda.house.biz.util.Transform;
import guda.house.biz.util.TransformHelper;
import guda.house.dal.dao.*;
import guda.house.dal.dataobject.BuildingDO;
import guda.house.dal.dataobject.SuiteDO;
import guda.house.dal.dataobject.SuiteOutUserRelDO;
import guda.house.dal.dataobject.UnitDO;
import guda.house.web.helper.ResponseHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by well on 16/3/15.
 */
@Controller
@RequestMapping(value = "sys/common")
public class CleanAction {

    @Autowired
    private VillageDAO villageDAO;
    @Autowired
    private BuildingDAO buildingDAO;
    @Autowired
    private UnitDAO unitDAO;
    @Autowired
    private SuiteDAO suiteDAO;
    @Autowired
    private TransactionTemplate transactionTemplate;
    @Autowired
    private OwnerDAO ownerDAO;
    @Autowired
    private RenderDAO renderDAO;
    @Autowired
    private ContactDAO contactDAO;


    @RequestMapping(value = "clean.htm", method = RequestMethod.GET)
    public String clean(HttpServletRequest request, ModelMap modelMap) {
        return "sys/common/clean.vm";

    }

    @RequestMapping(value = "doCleanVillage.htm")
    public void list(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) {
        long villageId = RequestUtil.getLong(request, "villageId");
        JsonResult result = JsonResult.getInstance();
        if(villageId == 0){
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        }
        BuildingQuery buildingQuery = new BuildingQuery();
        buildingQuery.setVillageId(villageId);
        buildingQuery.setPageSize(1000);
        final List<BuildingDO> buildingDOs = buildingDAO.selectForPage(buildingQuery);
        if(buildingDOs.size() == 0){
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        }
        List<Long> buildingIdList = TransformHelper.transform(buildingDOs, new Transform<BuildingDO, Long>() {
            @Override
            public Long transform(BuildingDO object) {
                return object.getBuildingId();
            }
        });
        UnitQuery unitQuery = new UnitQuery();
        unitQuery.setBuildingIdList(buildingIdList);
        unitQuery.setPageSize(5000);
        final List<UnitDO> unitDOs = unitDAO.selectForPage(unitQuery);

        if(unitDOs.size() == 0){
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        }

        List<Long> unitIdList = TransformHelper.transform(unitDOs, new Transform<UnitDO, Long>() {
            @Override
            public Long transform(UnitDO object) {
                return object.getUnitId();
            }
        });

        SuiteQuery suiteQuery = new SuiteQuery();
        suiteQuery.setSuiteIdList(unitIdList);
        suiteQuery.setPageSize(10000);
        final List<SuiteDO> suiteDOs = suiteDAO.selectForPage(suiteQuery);
        transactionTemplate.execute(new TransactionCallbackWithoutResult() {
            @Override
            protected void doInTransactionWithoutResult(TransactionStatus transactionStatus) {
                for(SuiteDO suiteDO:suiteDOs){
                    suiteDAO.delSuiteById(suiteDO.getSuiteId());
                    ownerDAO.delOwnerBySuiteId(suiteDO.getSuiteId());
                    contactDAO.delContactBySuiteId(suiteDO.getSuiteId());
                    renderDAO.delRenderBySuiteId(suiteDO.getSuiteId());
                }
                for(UnitDO unitDO:unitDOs){
                    unitDAO.delUnitById(unitDO.getUnitId());
                }
                for(BuildingDO buildingDO:buildingDOs){
                    buildingDAO.delBuildingById(buildingDO.getBuildingId());
                }
            }
        });

        ResponseHelper.ajaxReturn(result.getMap(), response);
        return;
    }

//    @RequestMapping(value = "doResetBuildingType.htm")
//    public void resetBuildingType(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) {
//        long villageId = RequestUtil.getLong(request, "villageId");
//        JsonResult result = JsonResult.getInstance();
//        if(villageId == 0){
//            ResponseHelper.ajaxReturn(result.getMap(), response);
//            return;
//        }
//        BuildingQuery buildingQuery = new BuildingQuery();
//        buildingQuery.setVillageId(villageId);
//        buildingQuery.setPageSize(1000);
//        final List<BuildingDO> buildingDOs = buildingDAO.selectForPage(buildingQuery);
//        if(buildingDOs.size() == 0){
//            ResponseHelper.ajaxReturn(result.getMap(), response);
//            return;
//        }
//
//
//        transactionTemplate.execute(new TransactionCallbackWithoutResult() {
//            @Override
//            protected void doInTransactionWithoutResult(TransactionStatus transactionStatus) {
//                for(BuildingDO buildingDO:buildingDOs){
//                    UnitQuery unitQuery = new UnitQuery();
//                    unitQuery.setBuildingId(buildingDO.getBuildingId());
//                    unitQuery.setPageSize(5000);
//                    final List<UnitDO> unitDOs = unitDAO.selectForPage(unitQuery);
//                    if(unitDOs.size() == 0){
//                        continue;
//                    }
//                    List<Long> unitIdList = TransformHelper.transform(unitDOs, new Transform<UnitDO, Long>() {
//                        @Override
//                        public Long transform(UnitDO object) {
//                            return object.getUnitId();
//                        }
//                    });
//
//                    SuiteQuery suiteQuery = new SuiteQuery();
//                    suiteQuery.setUnitIdList(unitIdList);
//                    suiteQuery.setPageSize(10000);
//                    final List<SuiteDO> suiteDOs = suiteDAO.selectForPage(suiteQuery);
//                    for(SuiteDO suiteDO:suiteDOs){
//                        suiteDO.setBuildingTypeId(buildingDO.getBuildingTypeId());
//                        suiteDAO.updateByIdSelective(suiteDO);
//                    }
//                }
//            }
//        });
//
//        ResponseHelper.ajaxReturn(result.getMap(), response);
//        return;
//    }
}
